package sevensoft.结构型模式.代理模式.cglib动态代理;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class ProxyFactory implements MethodInterceptor {

    private Station station = new Station();

    public Station getStation() {
        //创建Enhancer对象，相当于jdk代理的Proxy对象
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(Station.class);
        enhancer.setCallback(this);
        Station o = (Station) enhancer.create();
        return o;
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("gclib动态代理");
        Object invoke = method.invoke(station, objects);
        return invoke;
    }

    public static void main(String[] args) {
        LocalDate strat = LocalDate.of(2023, 9, 10);
        LocalDate localDate = LocalDate.of(2024, 1, 20);
        System.out.println(strat.until(localDate, ChronoUnit.DAYS));
    }

}
